package com.hunttown.mes.impl;

import com.hunttown.mes.service.AnalysisNoticeService;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisNoticeRpcService;
import com.hunttown.mes.rpc.domain.AnalysisNoticeDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisNoticeDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisNoticeConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:06:21
 */
@Service
public class AnalysisNoticeRpcServiceImpl implements AnalysisNoticeRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisNoticeRpcServiceImpl.class);

    private final AnalysisNoticeService analysisNoticeService;

    @Autowired
    public AnalysisNoticeRpcServiceImpl(AnalysisNoticeService analysisNoticeService) {
        this.analysisNoticeService = analysisNoticeService;
    }

    @Override
    public AnalysisNoticeDTO insert(AnalysisNoticeDTO analysisNoticeDTO) {
        try {
            return convert(analysisNoticeService.insert(convert(analysisNoticeDTO)));
        } catch (Exception e) {
            logger.error("AnalysisNotice create failed.", e);
            throw new RuntimeException("AnalysisNotice create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisNoticeDTO analysisNoticeDTO) {
        try {
            return analysisNoticeService.updateInfoById(convert(analysisNoticeDTO));
        } catch (Exception e) {
            logger.error("AnalysisNotice update by id failed.", e);
            throw new RuntimeException("AnalysisNotice update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisNoticeDTOQuery analysisNoticeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisNoticeDTOQuery.getW_id() != null && analysisNoticeDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisNoticeDTOQuery.getW_id());
            }

            if (analysisNoticeDTOQuery.getIds() != null && !analysisNoticeDTOQuery.getIds().equals("")) {
                query.put("ids", analysisNoticeDTOQuery.getIds());
            }

            if (analysisNoticeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisNoticeDTOQuery.getStopFlag());
            }

            if (analysisNoticeDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisNoticeDTOQuery.getDeleteFlag());
            }

            return analysisNoticeService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisNotice update by query failed.", e);
            throw new RuntimeException("AnalysisNotice update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisNoticeDTO selectById(Integer id) {
        try {
            return convert(analysisNoticeService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisNotice select by id failed.", e);
            throw new RuntimeException("AnalysisNotice select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisNoticeDTO selectObjByQuery(AnalysisNoticeDTOQuery analysisNoticeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(analysisNoticeService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisNotice select obj by query failed.", e);
            throw new RuntimeException("AnalysisNotice select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisNoticeDTO> getListForPage(AnalysisNoticeDTOQuery analysisNoticeDTOQuery) {
        Query query = Query.page(analysisNoticeDTOQuery.getPn(), analysisNoticeDTOQuery.getPageSize());

        if (analysisNoticeDTOQuery.getIds() != null && !analysisNoticeDTOQuery.getIds().equals("")) {
            query.put("ids", analysisNoticeDTOQuery.getIds());
        }

        if (analysisNoticeDTOQuery.getCatalogId() != null) {
            query.put("catalogId", analysisNoticeDTOQuery.getCatalogId());
        }

        if (analysisNoticeDTOQuery.getLikeName() != null && !analysisNoticeDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisNoticeDTOQuery.getLikeName());
        }

        if (analysisNoticeDTOQuery.getOrderBy() != null && analysisNoticeDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisNoticeDTOQuery.getOrderBy());
        }

        if (analysisNoticeDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisNoticeDTOQuery.getStopFlag());
        }

        if (analysisNoticeDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisNoticeDTOQuery.getDeleteFlag());
        }

        List<AnalysisNoticeDTO> list = convert(analysisNoticeService.selectByQuery(query));
        Page<AnalysisNoticeDTO> page = Page.from(analysisNoticeDTOQuery.getPn(), analysisNoticeDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisNoticeService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisNoticeService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisNotice delete failed.", e);
            throw new RuntimeException("AnalysisNotice delete failed, Please order your code.", e);
        }
    }
}